package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.DormManager;
import com.example.springboot.entity.DormRoom;
import com.example.springboot.entity.Student;
import com.example.springboot.entity.User;
import com.example.springboot.service.DormManagerService;
import com.example.springboot.service.DormRoomService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dormManager")
public class DormManagerController {

    @Resource
    private DormManagerService dormManagerService;
    @Resource
    private DormRoomService dormRoomService;

    /**
     * 宿管添加
     */
//    @PostMapping("/add")
//    public Result<?> add(@RequestBody DormManager dormManager) {
//        int i = dormManagerService.addNewDormManager(dormManager);
//        if (i == 1) {
//            return Result.success();
//        } else {
//            return Result.error("-1", "添加失败");
//        }
//    }

    /**
     * 添加学生信息
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody DormManager dormManager) {
        QueryWrapper<DormManager> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(DormManager::getUsername,dormManager.getUsername());
        DormManager one=dormManagerService.getOne(queryWrapper);
        if (one!=null){
            return Result.error("-1", "账户已存在");
        }
        dormManagerService.addDorm(dormManager);
        return Result.success();
    }

    /**
     * 宿管信息更新
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody DormManager dormManager) {
        if (StringUtils.equals("test",dormManager.getUsername())){
            return Result.error("-1", "禁止修改测试账号");
        }
        int i = dormManagerService.updateNewDormManager(dormManager);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "更新失败");
        }
    }

    /**
     * 宿管删除
     */
    @DeleteMapping("/delete/{username}")
    public Result<?> delete(@PathVariable String username) {
        if (StringUtils.equals("test",username)){
            return Result.error("-1", "禁止修改测试账号");
        }
        int i = dormManagerService.deleteDormManager(username);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "删除失败");
        }
    }

    /**
     * 宿管查找
     */
    @GetMapping("/find")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page page = dormManagerService.find(pageNum, pageSize, search);
        if (page != null) {
            return Result.success(page);
        } else {
            return Result.error("-1", "查询失败");
        }
    }

    /**
     * 宿管登录
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user, HttpSession session) {

        Object o = dormManagerService.dormManagerLogin(user.getUsername(), user.getPassword());
        if (o != null) {
            System.out.println(o);
            //存入session
            session.setAttribute("Identity", "dormManager");
            session.setAttribute("User", o);
            return Result.success(o);
        } else {
            return Result.error("-1", "用户名或密码错误");
        }
    }


    /**
     * 查找学生信息
     */
    @GetMapping("/findList")
    public Result<?> findList(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page page = dormManagerService.findList(pageNum, pageSize, search);
        if (page != null) {
            return Result.success(page);
        } else {
            return Result.error("-1", "查询失败");
        }
    }


    @GetMapping("/getdorm/{name}")
    public Result<?> getDormInfo(@PathVariable String name) {
        // 1. 检查宿舍是否存在
        DormRoom dormRoom = dormRoomService.judgeHadBed(name);
        if (dormRoom == null) {
            return Result.error("-1","宿舍不存在");
        }
        System.out.println(dormRoom);
        // 2. 查询该宿舍楼的管理员
        QueryWrapper<DormManager> qw = new QueryWrapper<>();
        qw.eq("dormbuild_id", dormRoom.getDormBuildId());
        // 3. 调用service查询（注意首字母小写）
        List<DormManager> dormManagers = dormManagerService.list(qw);
        // 4. 返回结果
        return Result.success(dormManagers);
    }

}
